package com.fausto.learnimprint.utils

import java.util.regex.Pattern

class FormatUtil{
    companion object{
        //邮箱格式
        private val MAIL_PATTERN: Pattern =
            Pattern.compile("\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}")
        //IP格式
        private val IP_PATTERN: Pattern   =
            Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}") //ip格式

        /**
         *      判断数字是否处于指定范围内, 默认为[0,1]
         */
        fun isIntRange(num: Int, rangeStart: Int = 0, rangeOver: Int = 1): Boolean{
            return when (num) {
                in rangeStart..rangeOver -> true
                else -> false
            }
        }

        /**
         *      判断邮箱格式是否正确
         */
        fun checkMail(mail: String): Boolean {
            val m = MAIL_PATTERN.matcher(mail)
            return m.matches()
        }

        /**
         *      判断IP格式是否正确
         */
        fun checkIP(name: String): Boolean {
            val m = IP_PATTERN.matcher(name)
            return m.matches()
        }

    }
}
